<template>
  <view>
    <view class="search-box">
      <!-- 使用 uni-ui 提供的搜索组件 -->
      <uni-search-bar @confirm="confirm" @input="input" :radius="100" focus cancelButton="none"></uni-search-bar>
      <!-- 
    防抖:只走最后一次 
    节流:单位时间只走第一次
     -->

      <view class="sugg-list" v-if="kw">
        <view class="sugg-item" v-for="(item, i) in searchResults" :key="i" @click="gotoDetail(item.goods_id)">
          <view class="goods-name">{{item.goods_name}}</view>
          <uni-icons type="arrowright" size="16"></uni-icons>
        </view>
      </view>
      <view class="history" v-else>

        <view class="" @click="delAll">
          清除所有
        </view>===
        <view class="" @click="showX=!showX">
          删除
        </view>
        <view class="history-item" v-for="(item,index) in  historyList" :key='index'>
          {{item}}===
          <text @click="del(index)" v-if="showX">×</text>
        </view>
      </view>
      <view class="" v-if="!historyList.length &&!kw">
        暂无历史数据
      </view>
    </view>
  </view>
</template>
<script>
  export default {
    data() {
      return {
        // 延时器的 timerId
        timer: null,
        // 搜索关键词
        kw: '',
        // 搜索结果列表  
        searchResults: [],
        historyList: uni.getStorageSync('historyList') || [],
        showX: false
      };
    },
    methods: {
      input(e) {
        // 防抖
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          // console.log(1111);
          this.kw = e
          if (this.kw) {
            this.getSuggest()
          }
        }, 1000)
      },
      async getSuggest() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/qsearch', {
          query: this.kw
        })
        if (res.meta.status !== 200) return uni.$showMsg()
        this.searchResults = res.message
      },
      gotoDetail(id) {
        uni.navigateTo({
          url: '../../subpkg/goods_detail/goods_detail?goods_id=' + id
        })
      },
      confirm() {
        // 表示按下enter键
        // console.log('confirm');
        /* 
        1-获取表单值
        2-将输入的表单值unshift到historyList
        3-需要做本地缓存====
        4-初次进来需要获取本地缓存 
        5-数据去空和去重
        6-删除单个和所有
        */
        if (!this.kw.trim()) return
        if (this.historyList.includes(this.kw.trim())) return
        this.historyList.unshift(this.kw.trim());
        uni.setStorageSync('historyList', this.historyList);
      },
      del(index) {
        //  arr=[1,2,3,4]====arr.splice(index,1)
        this.historyList.splice(index, 1)
        uni.setStorageSync('historyList', this.historyList);
      },
      delAll() {
        this.historyList = []
        uni.setStorageSync('historyList', this.historyList);
      }
    }
  }
</script>
<style lang="scss">
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
  }

  .uni-searchbar {
    display: flex;
    flex-direction: row;
    position: relative;
    padding: 16rpx;
    background-color: #c00000;
  }

  .sugg-list {
    padding: 0 5px;

    .sugg-item {
      font-size: 12px;
      padding: 13px 0;
      border-bottom: 1px solid #efefef;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .goods-name {
        // 文字不允许换行（单行文本）
        white-space: nowrap;
        // 溢出部分隐藏
        overflow: hidden;
        // 文本溢出后，使用 ... 代替
        text-overflow: ellipsis;
        margin-right: 3px;
      }
    }
  }

  .history {
    display: flex;

    .history-item {
      padding: 5px 10px;
      background-color: pink;
      margin: 5px;
    }
  }
</style>
